// 加上这段代码是为了让网页加载完了再去加载这些函数
window.onload = function () {
    const {createEditor, createToolbar} = window.wangEditor

    const editorConfig = {
        placeholder: '清输入内容...',
        onChange(editor) {
            const html = editor.getHtml()
            // console.log('editor content', html)
            // 也可以同步到 <textarea>
        },
    }

    const editor = createEditor({
        selector: '#editor-container',
        html: '<p><br></p>',
        config: editorConfig,
        mode: 'default', // or 'simple'
    })

    const toolbarConfig = {}

    const toolbar = createToolbar({
        editor,
        selector: '#toolbar-container',
        config: toolbarConfig,
        mode: 'default', // or 'simple'
    })


    // 博客发布
    $('#submit-btn').click(function (event) {
        // 不要让他走表单形式，需要通过ajax形式
        event.preventDefault()
        let title = $('input[name="title"]').val()
        let category = $('select[name="category"]').val()
        let content = editor.getHtml()
        let csrf_token = $('input[name="csrfmiddlewaretoken"]').val()
        $.ajax('/blog/pub',
            {
                method: 'POST',
                data: {title, category, content, csrfmiddlewaretoken: csrf_token},
                success: function (result) {
                    console.log('success',result)
                   if (result['code'] === '200') {
                      //  获取博客id
                       let blog_id=result['data']
                      window.location='/blog/detail/'+blog_id
                   }else {
                       alert(result['message'])
                   }
                }
                ,
                error: function (result) {
                    console.log("error", result);
                },
            }
        )
    })

}